package marchsoft.modules.tiku.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import marchsoft.annotation.rest.AnonymousDeleteMapping;
import marchsoft.annotation.rest.AnonymousGetMapping;
import marchsoft.annotation.rest.AnonymousPostMapping;
import marchsoft.annotation.rest.AnonymousPutMapping;
import marchsoft.base.PageVO;
import marchsoft.enums.ResultEnum;
import marchsoft.exception.BadRequestException;
import marchsoft.modules.tiku.entity.dto.AddSafeTypeDto;
import marchsoft.modules.tiku.entity.dto.ESafeTypeDto;
import marchsoft.modules.tiku.entity.dto.SafeTypeQueryCriteria;
import marchsoft.modules.tiku.mapper.ESafeTypeMapper;
import marchsoft.modules.tiku.validation.ValidateId;
import marchsoft.modules.tiku.validation.ValidateShow;

import marchsoft.response.Result;
import marchsoft.utils.SecurityUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.EscapedErrors;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

import marchsoft.modules.tiku.service.IESafeTypeService;
import marchsoft.modules.tiku.entity.ESafeType;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Set;


/**
* <p>
* 安全类别 前端控制器
* </p>
* @author zsy
* @since 2022-03-29
*/
@RequiredArgsConstructor
@RestController
@Slf4j
@Api(tags = "安全类别模块")
@RequestMapping("/api/eSafeType")
public class ESafeTypeController {
    private final IESafeTypeService eSafeTypeService;

    @ApiOperation("导出用户数据")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, SafeTypeQueryCriteria criteria) throws IOException {
        //每页条数设置为-1，是查询全部
        PageVO pageVO = new PageVO();
        pageVO.setSize(-1);
        Result<IPage<ESafeType>> pageResult = getAll(criteria, pageVO);
        List<ESafeType> eSafeTypeList= pageResult.getData().getRecords();
        eSafeTypeService.download(eSafeTypeList, response);
    }
    @AnonymousGetMapping("/front-all")
    public Result<Object> getAll(ESafeType eSafeType){
        List<ESafeType> front=eSafeTypeService.getFront(eSafeType);
        return Result.success(front);
    }
    @ApiOperation("获得所有的分类名称")
    @AnonymousGetMapping("/allName")
    public Result<Object> getAllName( ESafeType eSafeType){
        List<ESafeTypeDto> allName=eSafeTypeService.getAllName(eSafeType);
        return Result.success(allName);
    }


    @ApiOperation("根据条件获得分类信息")
    @AnonymousGetMapping
    public Result<IPage<ESafeType>> getAll(SafeTypeQueryCriteria criteria,PageVO pageVO){

        LambdaQueryWrapper<ESafeType> queryWrapper=new LambdaQueryWrapper<>();
         //当名称不为空时，进行查询
        if(StrUtil.isNotBlank(criteria.getName())){
            queryWrapper.like(ESafeType::getName,criteria.getName());
        }
        //自己写的方法
//        IPage<ESafeType> all=eSafeTypeService.getAll(criteria,pageVO);

        //mybatis-plus提供的分类展示方法
       IPage<ESafeType> all= eSafeTypeService.page(pageVO.buildPage(),queryWrapper);
        // mybatis-plus提供的方法（展示全部，但是不会分页）：
//        List<ESafeType> all=eSafeTypeService.list();
        return Result.success(all);
    }
    @ApiOperation("删除分类")
    @AnonymousDeleteMapping
    public Result<Object> deleteType(@RequestBody @Validated(ValidateId.class) Set<Long> ids){
//        if(CollectionUtil.isEmpty(ids)){
//            throw new BadRequestException(ResultEnum. PARAM_IS_BLANK);
//        }
//        eSafeTypeService.deleteByIds(ids);
        eSafeTypeService.removeByIds(ids);
        return Result.success();
    }
    @ApiOperation("新增分类")
    @AnonymousPostMapping
    public Result<Object> addType(@RequestBody @Validated AddSafeTypeDto eSafeType){
//        if(ObjectUtil.isNotNull(eSafeType.getId())){
//            throw new BadRequestException(ResultEnum.PARAM_IS_INVALID);
//        }
        eSafeTypeService.addSafeType(eSafeType);
        return Result.success();
    }
    @ApiOperation("修改分类")
    @AnonymousPutMapping
    public Result<Object> updateType(@RequestBody @Validated ESafeType eSafeType){
        eSafeTypeService.updateType(eSafeType);
        return Result.success();
    }
    @ApiOperation("修改状态")
    @AnonymousPutMapping("/update/state")
    public Result<Object> updateStatus( @RequestBody @Validated({ValidateId.class, ValidateShow.class}) ESafeType eSafeType){
        eSafeTypeService.updateStatus(eSafeType);
        return  Result.success();
    }

}
